package cn.biplam.setup.code.product;

import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.paramters.SysCaption;
import cn.biplam.common.paramters.SysName;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.front.component.AjxResponse;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlValue;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Clear_Type;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;
import cn.biplam.front.htmlscript.script.PopupScript;
import cn.biplam.front.htmlscript.script.AjxScript.Is_SubMitData;
import cn.biplam.front.template.base.Select;
import cn.biplam.front.template.base.SimpleInput;
import cn.biplam.front.template.base.SimpleTextArea;
import cn.biplam.front.template.control.Confirm;
import cn.biplam.front.template.control.FlatTable;
import cn.biplam.front.template.control.FlowTable;
import cn.biplam.front.template.control.table.TablePropertys;
import cn.biplam.front.template.control.table.TablePropertys.Col_Content_Type;
import cn.biplam.front.template.control.table.TableScript.Action_Type;
import cn.biplam.front.template.pageFrame.mdManage.AlertPrompt;
import cn.biplam.setup.code.product.ProductData.ProductStaus;

/**
 * 产品管理
 * @author XiaoRuxing Sep 29, 20099:36:26 AM
 * @version 1.0
 */
public class ProductManage extends AjxResponse{

	public static String cId="9105";
	public static String Get_Produc_Manage = cId+"01";
	private static String Add=cId+"02";
	private static String Edit=cId+"03";
	private static String Del=cId+"04";
	private static String Add_Opt=cId+"05";
	private static String Edit_Opt=cId+"06";
	private static String Del_Opt=cId+"07";
	
	private static String editContainerId = cId+"ec";
	
	@Override
	protected HtmlWindow ajxGet() throws ErrorMessage {
		if(actionId.equals(Get_Produc_Manage)){
			return get();
		}else if(actionId.equals(Add)){
			return getAddPanel();
		}else if(actionId.equals(Add_Opt)){
			return addOpt();
		}else if(actionId.equals(Del)){
			return getDelPanel();
		}else if(actionId.equals(Del_Opt)){
			return delOpt();
		}else if(actionId.equals(Edit)){
			return getEditPanel();
		}else if(actionId.equals(Edit_Opt)){
			return editOpt();
		}else{
			throw new ErrorMessage("actionid is not found:"+actionId+"!");
		}
	}

	/**
	 * 获取产品管理界面
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow get() throws ErrorMessage{
		HtmlWindow win = new HtmlDiv(cId);
		win.put(new HtmlValue(cId+"r",cId+"r",recordId));
		HtmlWindow button = ButtonComp.get(mUser, SysCaption.Create);
		button.bind(Window_Event.OnClick,
				AjxScript.getPr(cId, Add, Is_SubMitData.Submit));
		button.setClear(Clear_Type.Both);
		button.setMarginTop("5");
		win.put(button);
		HtmlWindow content = getContent();
		content.setClear(Clear_Type.Both);
		content.setMarginTop("5");
		win.put(content);
		return win;
	}
	
	/**
	 * 获取产品管理信息
	 */
	private HtmlWindow getContent() throws ErrorMessage{
		String mainKey = ProductData.PRODUCT_ID;
		TablePropertys property = getProperty();
		FlowTable table = new FlowTable(cId+"_content",mainKey,property);
		table.put(getData());
		HtmlWindow  win = table.get();
		return win;
	}
	
	private OperatorResult getData(){
		ProductData data = new ProductData();
		SqlResult sr = data.getAll();
		OperatorResult result = new OperatorResult();
		result.setSelectResult(sr);
		return result;
	}
	
	/**
	 * 获取属性
	 */
	private TablePropertys getProperty() throws ErrorMessage{
		TablePropertys property = new TablePropertys(sHomeUrl,mUser);
		property.putCol(ProductData.PRODUCT_NAME, "产品名称");
		property.putCol(ProductData.PRODUCT_DESC,"产品描述");
		property.putCol(ProductData.SERVICE_FLG, "产品状态",
				new ProductStatuCallback());
		property.putCol(ProductData.PRODUCT_ID,
				Col_Content_Type.NAME_ANCHOR,
				"操作",new ProductOptCallback());
		property.setColAction(ProductData.PRODUCT_ID,
				Action_Type.POP_SUBMIT_PR,Edit);
		property.setColAction(ProductData.PRODUCT_ID,
				Action_Type.POP_SUBMIT_PR,Del);
		return property;
	}
	
	/**
	 * 获取添加界面
	 * @throws ErrorMessage 
	 */
	private HtmlWindow getAddPanel() throws ErrorMessage{
		HtmlWindow win = getPanel(mUser, null);
		String script = AjxScript.getSpNoRefresh(editContainerId, Add_Opt);//获取添加操作脚本
		win.bind(SysName.PromptSubmitID, 
				Window_Event.OnClick, 
				PopupScript.prSubmit(script));//绑定提交脚本到弹出窗口确定按钮
		return win;
	}
	
	/**
	 * 获取删除界面
	 */
	private HtmlWindow getDelPanel() throws ErrorMessage{
		ProductData data  = new ProductData();
		boolean productHaveMod = data.getProductHaveModel(recordId);
		if(productHaveMod){
			AlertPrompt alert = new AlertPrompt("当前产品下有模块不能删除");
			return alert.get();
		}else{
			Confirm del = new Confirm("确认要删除产品?",recordId);
			del.bind(Del_Opt);
			return del.get();
		}
	}
	
	/**
	 * 获取修改界面
	 */
	private HtmlWindow getEditPanel() throws ErrorMessage{
		SqlResult sr = new ProductData().get(recordId);
		HtmlWindow win = getPanel(mUser, sr);
		String script = AjxScript.getSpNoRefresh(editContainerId,
				Edit_Opt);//编辑操作脚本
		win.bind(SysName.PromptSubmitID, 
				Window_Event.OnClick, 
				PopupScript.prSubmit(script));//绑定提交脚本到弹出窗口确定按钮
		return win;
	}
	
	private HtmlWindow getPanel(UserInfo mUser,SqlResult sr) throws ErrorMessage{
		
		String productName=null;
		String productDesc=null;
		String serviceFlag=null;
		
		if(TOOLS.exist(sr)){
			sr.first();
			sr.next();
			productName=sr.getString(ProductData.PRODUCT_NAME);
			productDesc=sr.getString(ProductData.PRODUCT_DESC);
			serviceFlag=sr.getString(ProductData.SERVICE_FLG);
		}
		
		if(serviceFlag==null){
			serviceFlag = ProductStaus.Start;
		}
		Select statuSelect = new Select(ProductData.SERVICE_FLG,serviceFlag,sHomeUrl);
		statuSelect.set(ProductStaus.Start, "发布");
		statuSelect.set(ProductStaus.Stop, "停用");
		
		FlatTable table = new FlatTable(editContainerId,recordId);
		table.putTh("产品名称",0,0);
		table.putTd(new SimpleInput(ProductData.PRODUCT_NAME,"50",productName,100).get(), 0, 1);
		table.putTh("产品状态",1,0);
		table.putTd(statuSelect.get(), 1, 1);
		table.putTh("产品描述",2,0);
		table.putTd(new SimpleTextArea(ProductData.PRODUCT_DESC,"48",productDesc).get(), 2, 1);
		table.setColSize(0, 10);
		HtmlWindow win = table.get();
		win.setStyleSize(400, 150);
		return win;
	}
	
	/**
	 * 添加操作
	 */
	private HtmlWindow addOpt() throws ErrorMessage{
		ProductData data = new ProductData();
		DataFilter filter = new DataFilter();
		filter.putAll(params);
		filter.put(ProductData.SERVICE_FLG, 
				params.getByKey(ProductData.SERVICE_FLG+"value"));
		data.add(filter, mUser);
		return getRefresh();
	}
	
	/**
	 * 删除操作
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow delOpt() throws ErrorMessage{
		ProductData data = new ProductData();
		data.del(recordId);
		return getRefresh();
	}
	
	/**
	 * 修改操作
	 * @return
	 * @throws ErrorMessage 
	 */
	private HtmlWindow editOpt() throws ErrorMessage{
		ProductData data = new ProductData();
		DataFilter filter = new DataFilter();
		filter.putAll(params);
		filter.put(ProductData.SERVICE_FLG, 
				params.getByKey(ProductData.SERVICE_FLG+"value"));
		filter.put(ProductData.PRODUCT_ID, recordId);
		data.update(filter);
		return getRefresh();
	}
	
	private HtmlWindow getRefresh() throws ErrorMessage{
		HtmlWindow win = new HtmlDiv();
		win.bind(AjxScript.getA(cId, Get_Produc_Manage));
		return win;
	}
}

